<template>
  <transition name="v-toast">
    <div class="toast-box" v-if="show">
      <div class="toast-mask"></div>

      <div class="toast-text">{{text}}</div>
    </div>
  </transition>
</template>

<script>
  export default {
    name: 'toast',
    props: {
      show: { // 是否显示此toast
        type: Boolean,
        default: false
      },

      text: { // 提醒文字
        type: String,
        default: 'loading'
      },

      time: { // 显示时间
        type: Number,
        default: 1000
      }
    },

    updated () { // 时间控制
      clearTimeout(this.timeout)

      this.timeout = setTimeout(() => {
        this.show = false
      }, this.time)
    }
  }
</script>

<style scoped lang="scss">
  .toast {
    &-box {
      bottom: 0;
      left: 0;
      position: fixed;
      right: 0;
      top: 0;
      z-index: 1111;
    }

    &-mask {
      position: absolute;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0);
    }

    &-text {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      background: rgba(0, 0, 0, .8);
      border-radius: 4px;
      text-align: center;
      color: #ffffff;
      font-size: 16px;
      padding: 10px;
    }
  }

  .v-toast-enter-active {
    animation: scale-in .5s;
  }

  .v-toast-leave-active {
    animation: scale-out .5s;
  }

  @keyframes scale-in {
    from {
      transform: scale(1.4);
      opacity: 0;
    }
    to {
      transform: scale(1);
      opacity: 1;
    }
  }

  @keyframes scale-out {
    0% {
      transform: scale(1);
      opacity: 1;
    }
    100% {
      transform: scale(0);
      opacity: 0;
    }
  }
</style>
